import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { Spin } from 'antd';
import { useAuth } from '../contexts/AuthContext';

const AuthGuard = ({ children }) => {
    const { isAuthenticated, isLoading } = useAuth();
    const location = useLocation();

    // 如果正在加载，显示加载中
    if (isLoading) {
        return (
            <>
                <style>{`
          @keyframes fadeIn {
            0% {
              opacity: 0;
              transform: translateY(20px);
            }
            100% {
              opacity: 1;
              transform: translateY(0);
            }
          }

          @keyframes breathe {
            0%, 100% {
              transform: scale(1);
            }
            50% {
              transform: scale(1.05);
            }
          }

          @keyframes shimmer {
            0% {
              background-position: -200px 0;
            }
            100% {
              background-position: calc(200px + 100%) 0;
            }
          }

          .loading-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
            position: relative;
            overflow: hidden;
          }

          .loading-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: 
              radial-gradient(circle at 20% 50%, rgba(120, 119, 198, 0.08) 0%, transparent 50%),
              radial-gradient(circle at 80% 20%, rgba(255, 134, 159, 0.08) 0%, transparent 50%),
              radial-gradient(circle at 40% 80%, rgba(99, 102, 241, 0.08) 0%, transparent 50%);
            animation: breathe 4s ease-in-out infinite;
          }

          .loading-card {
            position: relative;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 24px;
            padding: 48px 64px;
            text-align: center;
            box-shadow: 
              0 20px 40px rgba(0, 0, 0, 0.08),
              0 1px 3px rgba(0, 0, 0, 0.1);
            animation: fadeIn 0.6s ease-out;
            max-width: 400px;
            width: 90%;
          }

          .loading-text {
            margin-top: 24px;
            color: #475569;
            fontSize: 18px;
            fontWeight: 500;
            letter-spacing: 0.5px;
          }

          .loading-subtitle {
            margin-top: 8px;
            color: #94a3b8;
            fontSize: 14px;
            fontWeight: 400;
          }

          .loading-progress {
            margin-top: 32px;
            height: 2px;
            background: #e2e8f0;
            border-radius: 1px;
            overflow: hidden;
            position: relative;
          }

          .loading-progress::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            height: 100%;
            width: 60px;
            background: linear-gradient(90deg, transparent, #6366f1, transparent);
            animation: shimmer 1.5s ease-in-out infinite;
          }
        `}</style>

                <div className="loading-container">
                    <div className="loading-card">
                        <Spin
                            size="large"
                            style={{
                                color: '#6366f1',
                            }}
                        />
                        <div className="loading-text">
                            正在加载中...
                        </div>
                        <div className="loading-subtitle">
                            Tree Admin System
                        </div>
                        <div className="loading-progress"></div>
                    </div>
                </div>
            </>
        );
    }

    // 如果未登录，重定向到登录页
    if (!isAuthenticated) {
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    // 如果已登录，渲染子组件
    return children;
};

export default AuthGuard;
